package com.aliyun.heiyu.powermaster.server.dao.impl;

import com.aliyun.heiyu.common.util.JdbcUtil;
import com.aliyun.heiyu.powermaster.server.annotation.Transactional;
import com.aliyun.heiyu.powermaster.server.dao.ArmStateGridCollectInfoDao;
import com.aliyun.heiyu.powermaster.server.dao.DeviceServerDao;
import com.aliyun.heiyu.powermaster.server.dao.StateGridCollectInfoDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class StateGridCollectInfoImpl implements StateGridCollectInfoDao {

    private DeviceServerDao deviceServerDao=new DeviceServerDaoImpl();
    private ArmStateGridCollectInfoDao armStateGridCollectInfoDao =new ArmStateGridCollectInfoDaoImpl();
    @Override
    public Map<String, String> findOne(String sn) {
        Object[] params = new Object[1];
        params[0] = sn;
        String sql = "SELECT * FROM state_grid_collect_info WHERE server_sn=? ORDER BY id DESC LIMIT 0,1";
        Map<String, String> map = JdbcUtil.queryForMap(sql, params);
        return map;
    }

    @Override
    public Map<String, String> findBySn(String sn, Integer position) {
        //判断服务器是arm  或x86
        String serverType=deviceServerDao.selectServerType(sn);
        if("arm".equals(serverType)){
           return armStateGridCollectInfoDao.findBySn(sn,position);
        }else{
            Object[] params = new Object[2];
            params[0] = sn;
            params[1] = position;
            String sql = "SELECT * FROM state_grid_collect_info WHERE server_sn=? ORDER BY id DESC limit ?,1";
            Map<String, String> map = JdbcUtil.queryForMap(sql, params);
            return map;
        }
    }


    @Override
    public List<Map<String, String>> findBySnArray(String[] sns, String startTime, String endTime) {
        //sns为null或者为空 表示查询当前所有可采集服务器设备
        StringBuffer sqlBuffer = new StringBuffer("SELECT collect_time,frame_power,server_power,cpu_freq,cpu_usage,server_sn FROM state_grid_collect_info WHERE 1=1");
        ArrayList<String> paramList = new ArrayList<>();
        sqlBuffer.append(" AND collect_time BETWEEN ? AND ?");
        paramList.add(startTime);
        paramList.add(endTime);
        if(sns != null && sns.length != 0){
            int length = sns.length;
            sqlBuffer.append(" AND server_sn IN (");
            for(int i=0; i<length; i++){
                if(i == length -1){
                    sqlBuffer.append("?)");
                } else {
                    sqlBuffer.append("?,");
                }
                paramList.add(sns[i]);
            }
        } else {
            return null;
        }


        String sql = sqlBuffer.toString();
        Object[] params = paramList.toArray();
        List<Map<String, String>> list = JdbcUtil.queryForList_Map(sql, params);
        return list;
    }

    @Override
    public List<Map<String, String>> getServerInfos(String[] fields) {
        StringBuffer sqlBuffer = new StringBuffer();
        sqlBuffer.append("SELECT server_sn ");
        if(fields != null && fields.length != 0){
            int length = fields.length;
            for(int i=0; i<length; i++){
                sqlBuffer.append("," +fields[i]);
            }
        }
        sqlBuffer.append(" FROM state_grid_collect_info WHERE collect_time = (select max(collect_time) from state_grid_collect_info)");
        String sql = sqlBuffer.toString();
        List<Map<String, String>> list = JdbcUtil.queryForList_Map(sql,null);
        return list;
    }

    @Override
    public List<Map<String, String>> findBySnArray1(String[] sns) {
        if(sns == null || sns.length == 0){
            return null;
        }
        StringBuffer sqlBuffer = new StringBuffer("SELECT collect_time,frame_power,server_power,cpu_freq,cpu_usage,server_sn FROM state_grid_collect_info WHERE 1=1");
        int length = sns.length;
        if(length != 0){
            sqlBuffer.append(" AND server_sn IN (");
            for(int i=0; i<length; i++){
                if(i == length -1){
                    sqlBuffer.append("?)");
                } else {
                    sqlBuffer.append("?,");
                }
            }
        }
        Object[] params = sns;
        sqlBuffer.append(" ORDER BY id DESC limit "+length*2);
        String sql = sqlBuffer.toString();
        List<Map<String, String>> list = JdbcUtil.queryForList_Map(sql, params);
        return list;
    }

    @Override
    public List<Map<String, String>> findSizeInfo(int length) {
        StringBuffer sqlBuffer = new StringBuffer("SELECT collect_time,frame_power,server_power,cpu_freq,cpu_usage,server_sn FROM state_grid_collect_info WHERE 1=1");
        sqlBuffer.append(" ORDER BY collect_time DESC limit "+length*2);
        String sql = sqlBuffer.toString();
        List<Map<String, String>> resultList = JdbcUtil.queryForList_Map(sql, null);

        return resultList;
    }

    @Override
    public List<Map<String, String>> findSizeInfo(int length ,String[] serverIdArray) {
        Object[] param =new Object[serverIdArray.length];
        StringBuffer sqlBuffer = new StringBuffer(" SELECT collect_time,frame_power,server_power,cpu_freq,cpu_usage,server_sn FROM state_grid_collect_info WHERE 1=1");

        sqlBuffer.append(" and  server_sn in(");
        for(int i=0;i<serverIdArray.length;i++){
            if(i==serverIdArray.length-1){
                sqlBuffer.append("?");
            }else{
                sqlBuffer.append("?,");
            }
            param[i]=serverIdArray[i];
        }
        sqlBuffer.append(")");
        sqlBuffer.append(" ORDER BY collect_time DESC limit "+length*2+" ");
        String sql = sqlBuffer.toString();
        List<Map<String, String>> resultList = JdbcUtil.queryForList_Map(sql, param);
        return resultList;
    }

    @Override
    public Map<String, String> findMaxOne(String deviceId, String startTime, String endTime) {
        Object[] params = new Object[3];
        params[0] = deviceId;
        params[1] = startTime;
        params[2] = endTime;
        String sql = "SELECT * FROM state_grid_collect_info WHERE server_sn=? and collect_time between ? and ? ORDER BY cpu_usage DESC LIMIT 0,1";
        Map<String, String> map = JdbcUtil.queryForMap(sql, params);
        return map;
    }
    
    @Override
    public boolean insertPduInfos(String pduSn,int branch_current1, int branch_current2, int branch_current3, int branch_current4, int branch_current5, int branch_current6, int branch_current7, int branch_current8, int branch_current9, int branch_current10, int branch_current11, int branch_current12, int branch_current13, int branch_current14, int branch_current15, int branch_current16) {
        String sql = "insert into pdu_infos(pdu_sn,branch_current1,branch_current2,branch_current3,branch_current4,branch_current5," +
            "branch_current6,branch_current7,branch_current8,branch_current9,branch_current10,branch_current11,branch_current12," +
            "branch_current13,branch_current14,branch_current15,branch_current16)" +
            "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        Object[] params = new Object[17];
        params[0] = pduSn;
        params[1] = branch_current1;
        params[2] = branch_current2;
        params[3] = branch_current3;
        params[4] = branch_current4;
        params[5] = branch_current5;
        params[6] = branch_current6;
        params[7] = branch_current7;
        params[8] = branch_current8;
        params[9] = branch_current9;
        params[10] = branch_current10;
        params[11] = branch_current11;
        params[12] = branch_current12;
        params[13] = branch_current13;
        params[14] = branch_current14;
        params[15] = branch_current15;
        params[16] = branch_current16;
        return JdbcUtil.update(sql,params);
    }

    @Override
    @Transactional
    public Boolean replaceInsert(Map<String, String> map, String bName) throws SQLException {
        //map不能为null
        StringBuilder sql=new StringBuilder();
        sql.append("REPLACE INTO "+bName+" ");
        List<String> list =new ArrayList<>();
        StringBuilder keys = new StringBuilder();
        StringBuilder values = new StringBuilder();
        keys.append("(");
        values.append("(");

        for(Map.Entry<String, String> vo : map.entrySet()){
            if(vo.getValue()==null || vo.getValue().equals("")){
                continue;
            }
            keys.append("`"+vo.getKey()+"`");
            keys.append(",");
            values.append("?");
            values.append(",");
            list.add(vo.getValue());
        }

        Object[] params=list.toArray();
        char[] str1=keys.substring(0, keys.length()-1).toCharArray();
        char[] str2=values.substring(0, values.length()-1).toCharArray();
        StringBuilder keyssb=new StringBuilder();
        StringBuilder valuessb=new StringBuilder();
        valuessb.append(str2);
        keyssb.append(str1);
        keys=keyssb;
        values=valuessb;
        keys.append(")");
        values.append(")");
        sql.append(keys).append("VALUES").append(values);

        boolean result = com.aliyun.heiyu.powermaster.server.rotary.utils.JdbcUtil.update(sql.toString(), params);
        return result;
    }

}
